# MirrorOS Makefile
# Copyright 2008 Matthew Iselin

# location where the kernel binary is installed
INSTALL_FOLDER:=../../kernel/binary/

# compiler and flags
CC:=/usr/mattise/cross/bin/i586-pc-mattise-gcc
CFLAGS:=-I ./include -nostdlib -ffreestanding -fno-common -O0
# -fPIC

# linker and flags
LD:=/usr/mattise/cross/bin/i586-pc-mattise-ld
LDFLAGS:=-Map link.map -r -o

# project directories
PROJDIRS:=.

# assembler
AS:=/usr/mattise/cross/bin/i586-pc-mattise-as
AS_SRCS:=

# source and objects
SRCS:=$(shell find $(PROJDIRS) -maxdepth 1 -name "*.c" -type f)
OBJS:=$(patsubst %.asm,%.o,$(AS_SRCS)) $(patsubst %.c,%.o,$(SRCS))

# echo command location
ECHO:=echo

# dependiencies
DEPFILES:=$(patsubst %.c,%.d,$(SRCS))
ASDEPFILES:=$(patsubst %.asm,%.d,$(AS_SRCS))

# final binary filename
FINALBIN:=$(INSTALL_FOLDER)tty.ko

# target all builds the kernel
all: $(FINALBIN)

# install copies the binary to the install folder
install: $(FINALBIN)

# clean up, but don't remove the binary from the install folder
clean:
	rm $(shell find -iname "*.[oda]")
	rm $(FINALBIN)

# final binary target
$(FINALBIN) : $(OBJS)
	$(LD) $(LDFLAGS) $(FINALBIN) $(OBJS)
	(cd ../../kernel/binary; ./mrdc minish tty.ko fat.ko ata.ko; cd ../../drivers/fat)

# setup the dependencies (so that header changes update relevant source)
-include $(DEPFILES)

# c++ source files
%.o : %.c
	$(CC) $(CFLAGS) -MMD -MP -MT "$*.d" -c $< -o $@

# assembly
%.o : %.asm
	$(AS) $< -o $@
